﻿using Liju.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace LiJu.Db
{
    public class TaskDbHelper
    {
        /// <summary>
        /// 添加产品任务
        /// </summary>
        /// <returns></returns>
        public static bool AddProductTask(TaskModel taskModel)
        {
            string strMySql = $"INSERT INTO task (WorkNum, Num, ProductId, ProductImgNum, ScrewControlLocation, ScrewLocation, ErrorLimit,RadiusError, ScrewDriverParam, SleeveModel, ScrewModel, Skip,TightenPrepare)VALUES({taskModel.WorkNum},{taskModel.Num}, {taskModel.ProductId}, {taskModel.ProductImgNum}, '{taskModel.ScrewControlLocation}', '{taskModel.ScrewLocation}', {taskModel.ErrorLimit}, {taskModel.RadiusError},'{taskModel.ScrewDriverParam}', '{taskModel.SleeveModel}', '{taskModel.ScrewModel}', {taskModel.Skip}, {taskModel.TightenPrepare}); ";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        /// <summary>
        /// 修改任务
        /// </summary>
        /// <returns></returns>
        public static bool UpdateProductTask(TaskModel taskModel)
        {
            string strMySql = $"update task set WorkNum={taskModel.WorkNum}, Num={taskModel.Num}, ProductId={taskModel.ProductId}, ProductImgNum={taskModel.ProductImgNum},ScrewControlLocation= '{taskModel.ScrewControlLocation}', ScrewLocation='{taskModel.ScrewLocation}',ErrorLimit={taskModel.ErrorLimit},RadiusError={taskModel.RadiusError},  ScrewDriverParam='{taskModel.ScrewDriverParam}', SleeveModel={taskModel.SleeveModel},ScrewModel= {taskModel.ScrewModel},Skip= {taskModel.Skip},TightenPrepare={taskModel.TightenPrepare} where Id={taskModel.Id}";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        /// <summary>
        /// 查询当前工序的所有任务
        /// </summary>
        /// <returns></returns>
        public static List<TaskModel> GetAllTaskByProductId(long ProductId,int GongXuNo)
        {
            //获取没有删除的产品
            string strMySql = $"select * from task where ProductId={ProductId} and WorkNum={GongXuNo} " +
                $"and (Status!=2  or Status is Null) order by ProductImgNum,Num";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var products = DbExtension.DataTableToList<TaskModel>(dt);

            return products;
        }

        /// <summary>
        /// 查询所有任务,以Num排序
        /// </summary>
        /// <returns></returns>
        public static List<TaskModel> GetAllTaskByProductIdOrderNum(long ProductId)
        {
            //获取没有删除的产品
            string strMySql = $"select * from task where ProductId={ProductId} and Status!=2  or Status is Null order by WorkNum,Num";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var products = DbExtension.DataTableToList<TaskModel>(dt);

            return products;
        }

        /// <summary>
        /// 删除产品
        /// 0:禁用 1:启用 2:删除
        /// </summary>
        public static bool DeleteTaskByProductId(long ProductId)
        {
            string strMySql = $"update task  set Status=2 where ProductId={ProductId};";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        /// <summary>
        /// 删除产品
        /// 0:禁用 1:启用 2:删除
        /// </summary>
        public static bool DeleteTaskByTaskId(long taskId)
        {
            string strMySql = $"update task  set Status=2 where Id={taskId};";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        /// <summary>
        /// 查询指定任务
        /// </summary>
        /// <returns></returns>
        public static TaskModel GetTaskByID(long TaskID)
        {
            //获取没有删除的产品
            string strMySql = $"select * from task where ID={TaskID} ";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var listTask = DbExtension.DataTableToList<TaskModel>(dt);
            if (listTask.Count > 0)
            {
                return listTask[0];
            }
            else
            {
                return null;
            }
        }
    }
}
